package com.learning.thread.optimize;

/**
 * ClassName: LockCoarsening
 * Description: 锁粗化
 * Date: 2018/7/17 11:35 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class LockCoarsening {

    /**
     * 多次锁的请求
     */
    public void syncMethod() {
        synchronized (this) {
            oneMethod();
        }

        secondMethod();

        synchronized (this) {
            threadMethod();
        }
    }


    /**
     * 整合成一次锁请求
     */
    public void optimizeSyncMethod() {
        synchronized (this) {
            oneMethod();
            secondMethod();
            threadMethod();
        }
    }

    /**
     * 反面例子
     */
    public void badMethod() {
        for (int i = 0; i < 10; i++) {
            synchronized (this) {
                // do something
            }
        }
    }


    private void threadMethod() {
    }

    private void secondMethod() {
    }

    private void oneMethod() {
    }
}
